<template>
	<basic-container>
		<avue-crud :option="option" :data="data" ref="crud" v-model="form" :table-loading="loading" :permission="permissionList"
		@row-del="rowDel" @row-update="rowUpdate" @row-save="rowSave" :page="page" @selection-change="selectionChange"
		@refresh-change="refreshChange" @search-change="searchChange" @search-reset="searchReset" @current-change="currentChange"
		@size-change="sizeChange" @on-load="onLoad">
			<template slot="menuLeft">
				<!--<el-button type="primary" size="mini" @click="delAll()">批量删除</el-button>-->
			</template>
		</avue-crud>
	</basic-container>
</template>

<script>
	import {
		getList,
		remove,
		update,
		add,
		deletes
	} from "@/api/base/store/bank";
	import {
		validateFormat
	} from "@/util/util";
	import {
		mapGetters
	} from "vuex";

	export default {
		data() {
			return {
				idList: [],
				form: {},
				query: {},
				loading: true,
				rolesTree: [],
				rolesKeys: [],
				box: false,
				page: {
					pageSize: 10,
					currentPage: 1,
					total: 0
				},
				option: {
					size: 'mini',
					excelBtn: false,
					tip: false,
					border: true,
					index: true,
					// selection: true,
					viewBtn: true,
					rowKey: 'id',
					height: 'auto',
					// calcHeight: 380,
					dialogType: 'dialog',
					menuWidth: 300,
					column: [{
						label: "银行名称",
						prop: "bankName",
						fixed: true,
						maxlength: 15,
						rules: [{
							required: true,
							message: "请输入银行名称",
							trigger: "blur"
						},{
							required: true,
							validator:validateFormat,
							trigger:'blur'
						}]
					}, ]
				},
				data: []
			};
		},
		computed: {
			...mapGetters(["permission", 'userInfo']),
			permissionList() {
				return {
					addBtn: this.vaildData(this.permission["/mall-cloud-base/bank/addItem"], false),
					viewBtn: this.vaildData(this.permission["/mall-cloud-base/bank/detail"], false),
					delBtn: this.vaildData(this.permission["/mall-cloud-base/bank/delete"], false),
					editBtn: this.vaildData(this.permission["/mall-cloud-base/bank/editItem"], false)

				};
			},
		},
		methods: {
			//监控多选点击事件
			selectionChange(row) {
				var idList = [];
				if (row.length > 0) {
					for (let item of row) {
						idList.push(item.id)
					}
					this.idList = idList
				}
			},
			delAll() {
				if (this.idList.length == 0) {
					this.$message.error("请先选择要删除的银行")
					return;
				}
				deletes(this.idList).then(res => {
					if (res.data.code == 200) {
						this.refreshChange();
						this.$message({
							type: "success",
							message: "删除成功!"
						});
					} else {
						this.$message.error(res.data.message);
					}
				})
			},
			rowSave(row, loading, done) {
				row.position = row.positionIds;
				row.deptName = row.$deptId;
				row.repassword = row.password;
				add(row).then(() => {
					loading();
					this.onLoad(this.page);
					this.$message({
						type: "success",
						message: "操作成功!"
					});
				}, error => {
					done();
					console.log(error)
				});
			},
			rowUpdate(row, index, loading, done) {
				row.position = row.positionIds;
				update(row).then(() => {
					loading();
					this.onLoad(this.page);
					this.$message({
						type: "success",
						message: "操作成功!"
					});
				}, error => {
					done();
					console.log(error)
				});
			},
			rowDel(row) {
				this.$confirm("确定将选择数据删除?", {
						confirmButtonText: "确定",
						cancelButtonText: "取消",
						type: "warning"
					})
					.then(() => {
						return remove(row.id);
					})
					.then(res => {
                        if(res.data.success){
                            this.page.currentPage = 1;
                            this.onLoad(this.page);
                            this.$message({
                                type: "success",
                                message: "操作成功!"
                            });
						}
						else{
                            this.$message.error(res.data.message);
						}
					});
			},
			searchReset() {
				this.idList = []
				this.$refs.crud.toggleSelection();
				this.query = {};
				this.onLoad(this.page);
			},
			searchChange(params,done) {
				this.page.currentPage=1
				this.idList = []
				this.$refs.crud.toggleSelection();
				this.query = params;
				this.onLoad(this.page, params,done);
			},
			currentChange(currentPage) {
				this.page.currentPage = currentPage;
			},
			sizeChange(pageSize) {
				this.page.pageSize = pageSize;
			},
			refreshChange() {
				this.idList = []
				this.$refs.crud.toggleSelection();
				this.onLoad(this.page);
			},
			onLoad(page, params = {},done) {
				this.loading = true;
				getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
					if(done){
						done();
					}
					this.loading = false;
					const data = res.data;
					this.page.total = data.count;
					this.data = data.data;
				});
			}
		}
	};
</script>

<style>
</style>
